#ifndef GTL_ENGINE_STATE_HEADER_GUARD
#define GTL_ENGINE_STATE_HEADER_GUARD

#include <GTL/gtlAutomember.h>
#include "gtlEvent.h"

namespace GTL { namespace Engine {

	struct State;
	class GameObject;

	struct StateHolder : public GTL::AutoMembers::AutoMember
	{
		const char* StateName; 

		virtual int GetType() { return 'stat'; }
		virtual void CreateState() {};

		virtual State* GetState() = 0;
		virtual StateHolder* GetParentState() = 0;

		StateHolder* FindState(const char* state);
		Event::EventHandler* GetEvent(const char* name, bool noParentEvents);
	};

	struct State
	{
		StateHolder* Holder;
		virtual GameObject* GetParent() = 0;
	};

	template <typename PARENT_TYPE>
	struct StateTemplate : public State
	{
		PARENT_TYPE* Parent;

		virtual GameObject* GetParent() { return (GameObject*)Parent; }
	};
	inline bool operator==(const State& st, const State* stptr) { return stptr == &st; }
	inline bool operator==(const State* st, const State& stptr) { return st == &stptr; }

#define state(name) \
	struct __state_##name; \
	struct __state_##name##_Holder : public StateHolder \
	{ \
		StateTemplate<self_type>* TheState; \
		__state_##name##_Holder() { \
			TheState = new __state_##name; \
			AddAutoMemberToPostOrderList(); \
			StateName = #name; \
			TheState->Parent = (self_type*)ParentProducible; \
			TheState->Holder = this; \
		} \
		StateTemplate<self_type>* operator->() { return TheState; } \
		operator StateTemplate<self_type>*() { return TheState; } \
		virtual State* GetState() { return TheState; } \
		virtual StateHolder* GetParentState() { if (ParentMember->Is('stat')) return (StateHolder*)ParentMember; else return 0; } \
		\
		void Destroy() { delete TheState; TheState = 0; } \
		~__state_##name##_Holder() { Destroy(); } \
	} name; \
	struct __state_##name : public StateTemplate<self_type>
}}
#endif 